const cloud = require('wx-server-sdk')

cloud.init()

exports.main = async (event, context) => {
    const id = event._id;
    const fun_name = event.fun_name;
    const collectionName = event.collection_name;
    switch (fun_name) {
        case 'update_video_look_num':
            return update_video_look_num(collectionName, id);
        default:
            return {
                errMsg: '云函数没有此方法'
            }
    }
}

async function update_video_look_num (collection_name, ID) {
    const _ = cloud.database().command;
    try {
        return cloud.database().collection(collection_name).doc(ID).update({
            data: {
                look_num: _.inc(1)
            },
        })
    } catch (error) {
        cloud.logger().error({
            error
        });
        return error;
    }
}
